using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using BrainTechLLC;

namespace Mud.EmlenMudMain
{
    [Serializable]
    public class OldSchoolObjPrototypes
    {
        public OldSchoolObjPrototypes() { }

        public void fread_object_old_format(StreamReader sr, int vnum, AreaData area)
        {
            ObjectPrototype pObjIndex = new ObjectPrototype();
            pObjIndex.Max_in_room = 1;
            pObjIndex.Vnum = vnum;
            pObjIndex.Timer = 0;
            pObjIndex.area = area;
            pObjIndex.How_many = 0;
            pObjIndex.Objprotoname = "";
            pObjIndex.Short_descr = "";
            pObjIndex.Description = "";
            pObjIndex.Action_descr[0] = "";
            pObjIndex.Action_descr[1] = "";
            pObjIndex.Action_descr[2] = "";
            pObjIndex.Extra_flags = 0;
            pObjIndex.Level = 1;
            pObjIndex.Effectbits = 0;
            for (int tmpi = 0; tmpi < 10; tmpi++) pObjIndex.Value[tmpi] = 0;
            for (int tmpi = 0; tmpi < 10; tmpi++) pObjIndex.Values_2[tmpi] = 0;
            char[] spc = new char[1];
            spc[0] = ' ';

            while (sr.EndOfStream == false)
            {
                string szLine = sr.ReadLine().Trim();
                string[] rgsz = szLine.Split(spc, 2, StringSplitOptions.RemoveEmptyEntries);
                if (rgsz.Length == 0) continue;
                string szWord = rgsz[0];


                if (szWord == "Extra") { pObjIndex.Extra_flags = (ItemProperties)Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "ExtraDescr")
                {
                    ExtraDescriptionData ed = new ExtraDescriptionData();
                    ed.Keyword = rgsz[1].TrimEnd('~');
                    ed.Description = "";
                    string szLine2 = sr.ReadLine();
                    while (szLine2.Trim() != "~")
                    {
                        ed.Description += szLine2 + "\n\r";
                        szLine2 = sr.ReadLine();
                    }
                    pObjIndex.Extra_descr.Add(ed);
                    continue;
                }
                if (szWord == "End")
                {
                    EmlenMud.WorldTemplates.AllObjPrototypes.Add(vnum, pObjIndex);
                    area.ObjectPrototypesBelongingToArea.Add((ObjectPrototype)pObjIndex);
                    return;
                }
                if (szWord == "Affect")
                {
                    string[] rgSpl5 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
                    EffectData paf = new EffectData();
                    paf.ApplyEffectLocation = (ApplyEffect)Convert.ToInt32(rgSpl5[0]);
                    paf.ApplyEffectModifierValue = Convert.ToInt32(rgSpl5[1]);
                    paf.GeneralTypeOfEffect = Convert.ToInt32(rgSpl5[2]);
                    paf.EffectLastsUntil = DateTime.Now.AddMinutes(Convert.ToInt32(rgSpl5[3]));
                    paf.EffectTypeFlags = (long)Convert.ToInt32(rgSpl5[4]);
                    pObjIndex.Effects.Add(paf);
                    continue;
                }

                if (szWord == "Descr") { pObjIndex.Description = sr.ReadLine().Trim(); continue; }
                if (szWord == "DescA1") { pObjIndex.Action_descr[0] = rgsz[1].TrimEnd('~'); continue; }
                if (szWord == "DescA2") { pObjIndex.Action_descr[1] = rgsz[1].TrimEnd('~'); continue; }
                if (szWord == "DescA3") { pObjIndex.Action_descr[2] = rgsz[1].TrimEnd('~'); continue; }

                if (szWord == "Limited") continue;
                if (szWord == "Limtd") { pObjIndex.How_many = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "JavO") continue; // { pObjIndex.objImage[0] = (char)Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "JavT") continue; // { pObjIndex.objImage[1] = (char)Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "DMat") { pObjIndex.Made_of = rgsz[1]; continue; }
                if (szWord == "Level") { pObjIndex.Level = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "LEffect") { pObjIndex.Effectbits = (long)Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Timer") { pObjIndex.Timer = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Type")
                {
                    ItemType nOldItemType = (ItemType)Convert.ToInt32(rgsz[1]);
                    switch (nOldItemType)
                    {
                        case ItemType.ARMOR:
                            pObjIndex.Item_type = ItemTypes.ARMOR;
                            break;
                        case ItemType.BLOOD:
                            pObjIndex.Item_type = ItemTypes.BLOOD;
                            break;
                        case ItemType.BOAT:
                            pObjIndex.Item_type = ItemTypes.BOAT;
                            break;
                        case ItemType.BOOK:
                            pObjIndex.Item_type = ItemTypes.BOOK;
                            break;
                        case ItemType.CLIMBABLE:
                            pObjIndex.Item_type = ItemTypes.CLIMBABLE;
                            break;
                        case ItemType.CONTAINER:
                            pObjIndex.Item_type = ItemTypes.CONTAINER;
                            break;
                        case ItemType.CORPSE_NPC:
                            pObjIndex.Item_type = ItemTypes.CORPSE_NPC;
                            break;
                        case ItemType.CORPSE_PC:
                            pObjIndex.Item_type = ItemTypes.CORPSE_PC;
                            break;
                        case ItemType.DRINK_CON:
                            pObjIndex.Item_type = ItemTypes.DRINK_CON;
                            break;
                        case ItemType.FOOD:
                            pObjIndex.Item_type = ItemTypes.FOOD;
                            break;
                        case ItemType.FOUNTAIN:
                            pObjIndex.Item_type = ItemTypes.FOUNTAIN;
                            break;
                        case ItemType.FURNITURE:
                            pObjIndex.Item_type = ItemTypes.FURNITURE;
                            break;
                        case ItemType.GEM:
                            pObjIndex.Item_type = ItemTypes.GEM;
                            break;
                        case ItemType.KEY:
                            pObjIndex.Item_type = ItemTypes.KEY;
                            break;
                        case ItemType.LIGHT:
                            pObjIndex.Item_type = ItemTypes.LIGHT;
                            break;
                        case ItemType.LIST:
                            pObjIndex.Item_type = ItemTypes.LIST;
                            break;
                        case ItemType.MAP:
                            pObjIndex.Item_type = ItemTypes.MAP;
                            break;
                        case ItemType.PILL:
                            pObjIndex.Item_type = ItemTypes.PILL;
                            break;
                        case ItemType.POTION:
                            pObjIndex.Item_type = ItemTypes.POTION;
                            break;
                        case ItemType.REPELLANT:
                            pObjIndex.Item_type = ItemTypes.REPELLANT;
                            break;
                        case ItemType.SCALABLE:
                            pObjIndex.Item_type = ItemTypes.SCALABLE;
                            break;
                        case ItemType.SCALE_DEV:
                            pObjIndex.Item_type = ItemTypes.SCALE_DEV;
                            break;
                        case ItemType.SCROLL:
                            pObjIndex.Item_type = ItemTypes.SCROLL;
                            break;
                        case ItemType.STAFF:
                            pObjIndex.Item_type = ItemTypes.STAFF;
                            break;
                        case ItemType.TICKET:
                            pObjIndex.Item_type = ItemTypes.TICKET;
                            break;
                        case ItemType.TOOL:
                            pObjIndex.Item_type = ItemTypes.TOOL;
                            break;
                        case ItemType.TRASH:
                            pObjIndex.Item_type = ItemTypes.TRASH;
                            break;
                        case ItemType.TREASURE:
                            pObjIndex.Item_type = ItemTypes.TREASURE;
                            break;
                        case ItemType.VEHICLE:
                            pObjIndex.Item_type = ItemTypes.VEHICLE;
                            break;
                        case ItemType.WAND:
                            pObjIndex.Item_type = ItemTypes.WAND;
                            break;
                        case ItemType.WEAPON:
                            pObjIndex.Item_type = ItemTypes.WEAPON;
                            break;
                        case ItemType.WHATIS20:
                            pObjIndex.Item_type = ItemTypes.NONE;
                            break;
                    }
                    // string szItemType = Enum.GetName(typeof(ItemType), nOldItemType);
                    //  Enum.Parse(typeof(ItemTypes), szItemType, true);
                    continue;
                }
                if (szWord == "Maxinroom") { pObjIndex.Max_in_room = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Wear") { pObjIndex.Wear_flags = (WearPosition)Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Weight") { pObjIndex.Weight = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Cost") { pObjIndex.Cost = Convert.ToInt32(rgsz[1]); continue; }
                if (szWord == "Name") { pObjIndex.Objprotoname = rgsz[1].TrimEnd('~'); continue; }
                if (szWord == "Short") { pObjIndex.Short_descr = rgsz[1].TrimEnd('~'); continue; }
                if (szWord == "Values")
                {
                    string[] rgSpl2 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
                    for (int iHash = 0; iHash < 10; iHash++)
                    {
                        pObjIndex.Value[iHash] = Convert.ToInt32(rgSpl2[iHash]);
                    }
                    continue;
                }
                if (szWord == "Values2")
                {
                    string[] rgSpl2 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
                    for (int iHash = 0; iHash < 10; iHash++)
                    {
                        pObjIndex.Values_2[iHash] = Convert.ToInt32(rgSpl2[iHash]);
                    }
                    continue;
                }
            }

            return;
        }

        public void WriteOldObjectFormat(System.IO.StreamWriter sw, int vnum, ObjectPrototype op, AreaData area)
        {
            sw.WriteLine("#" + vnum.ToString());
            sw.WriteLine("Name " + op.Objprotoname + "~");
            sw.WriteLine("Short " + op.Short_descr + "~");
            sw.WriteLine("Descr");
            sw.WriteLine(op.Description);
            if (!(op.Description.EndsWith("\n") || op.Description.EndsWith("\r")))
            {
                sw.WriteLine("");
            }
            if ((op.Item_type & ItemTypes.ARMOR) != 0)
                sw.WriteLine("Type " + ((int)ItemType.ARMOR).ToString());
            else if ((op.Item_type & ItemTypes.BLOOD) != 0)
                sw.WriteLine("Type " + ((int)ItemType.BLOOD).ToString());
            else if ((op.Item_type & ItemTypes.BOAT) != 0)
                sw.WriteLine("Type " + ((int)ItemType.BOAT).ToString());
            else if ((op.Item_type & ItemTypes.BOOK) != 0)
                sw.WriteLine("Type " + ((int)ItemType.BOOK).ToString());
            else if ((op.Item_type & ItemTypes.CLIMBABLE) != 0)
                sw.WriteLine("Type " + ((int)ItemType.CLIMBABLE).ToString());
            else if ((op.Item_type & ItemTypes.CONTAINER) != 0)
                sw.WriteLine("Type " + ((int)ItemType.CONTAINER).ToString());
            else if ((op.Item_type & ItemTypes.CORPSE_NPC) != 0)
                sw.WriteLine("Type " + ((int)ItemType.CORPSE_NPC).ToString());
            else if ((op.Item_type & ItemTypes.CORPSE_PC) != 0)
                sw.WriteLine("Type " + ((int)ItemType.CORPSE_PC).ToString());
            else if ((op.Item_type & ItemTypes.DRINK_CON) != 0)
                sw.WriteLine("Type " + ((int)ItemType.DRINK_CON).ToString());
            else if ((op.Item_type & ItemTypes.FOOD) != 0)
                sw.WriteLine("Type " + ((int)ItemType.FOOD).ToString());
            else if ((op.Item_type & ItemTypes.FOUNTAIN) != 0)
                sw.WriteLine("Type " + ((int)ItemType.FOUNTAIN).ToString());
            else if ((op.Item_type & ItemTypes.FURNITURE) != 0)
                sw.WriteLine("Type " + ((int)ItemType.FURNITURE).ToString());
            else if ((op.Item_type & ItemTypes.GEM) != 0)
                sw.WriteLine("Type " + ((int)ItemType.GEM).ToString());
            else if ((op.Item_type & ItemTypes.KEY) != 0)
                sw.WriteLine("Type " + ((int)ItemType.KEY).ToString());
            else if ((op.Item_type & ItemTypes.LIGHT) != 0)
                sw.WriteLine("Type " + ((int)ItemType.LIGHT).ToString());
            else if ((op.Item_type & ItemTypes.LIST) != 0)
                sw.WriteLine("Type " + ((int)ItemType.LIST).ToString());
            else if ((op.Item_type & ItemTypes.MAP) != 0)
                sw.WriteLine("Type " + ((int)ItemType.MAP).ToString());
            else if ((op.Item_type & ItemTypes.PILL) != 0)
                sw.WriteLine("Type " + ((int)ItemType.PILL).ToString());
            else if ((op.Item_type & ItemTypes.POTION) != 0)
                sw.WriteLine("Type " + ((int)ItemType.POTION).ToString());
            else if ((op.Item_type & ItemTypes.REPELLANT) != 0)
                sw.WriteLine("Type " + ((int)ItemType.REPELLANT).ToString());
            else if ((op.Item_type & ItemTypes.SCALABLE) != 0)
                sw.WriteLine("Type " + ((int)ItemType.SCALABLE).ToString());
            else if ((op.Item_type & ItemTypes.SCALE_DEV) != 0)
                sw.WriteLine("Type " + ((int)ItemType.SCALE_DEV).ToString());
            else if ((op.Item_type & ItemTypes.SCROLL) != 0)
                sw.WriteLine("Type " + ((int)ItemType.SCROLL).ToString());
            else if ((op.Item_type & ItemTypes.STAFF) != 0)
                sw.WriteLine("Type " + ((int)ItemType.STAFF).ToString());
            else if ((op.Item_type & ItemTypes.TICKET) != 0)
                sw.WriteLine("Type " + ((int)ItemType.TICKET).ToString());
            else if ((op.Item_type & ItemTypes.TOOL) != 0)
                sw.WriteLine("Type " + ((int)ItemType.TOOL).ToString());
            else if ((op.Item_type & ItemTypes.TRASH) != 0)
                sw.WriteLine("Type " + ((int)ItemType.TRASH).ToString());
            else if ((op.Item_type & ItemTypes.TREASURE) != 0)
                sw.WriteLine("Type " + ((int)ItemType.TREASURE).ToString());
            else if ((op.Item_type & ItemTypes.VEHICLE) != 0)
                sw.WriteLine("Type " + ((int)ItemType.VEHICLE).ToString());
            else if ((op.Item_type & ItemTypes.WAND) != 0)
                sw.WriteLine("Type " + ((int)ItemType.WAND).ToString());
            else if ((op.Item_type & ItemTypes.WEAPON) != 0)
                sw.WriteLine("Type " + ((int)ItemType.WEAPON).ToString());
            /*						case ItemType.WHATIS20:
                          pObjIndex.Item_type = ItemTypes.NONE;*/
            sw.WriteLine("Wear " + ((int)op.Wear_flags).ToString());
            sw.WriteLine("Weight " + op.Weight.ToString());
            sw.WriteLine("Cost " + op.Cost.ToString());
            sw.WriteLine("Maxinroom " + op.Max_in_room.ToString());
            sw.Write("Values2");
            for (int i = 0; i < 10; i++)
            {
                sw.Write(" " + op.Values_2[i].ToString());
            }
            sw.WriteLine("");
            sw.Write("Values");
            for (int i = 0; i < 10; i++)
            {
                sw.Write(" " + op.Value[i].ToString());
            }
            sw.WriteLine("");
            sw.WriteLine("Extra " + ((int)op.Extra_flags).ToString());

            if (op.Extra_descr != null)
            {
                for (int i = 0; i < op.Extra_descr.Count; i++)
                {
                    ExtraDescriptionData ed = (ExtraDescriptionData)op.Extra_descr[i];
                    sw.WriteLine("ExtraDescr " + ed.Keyword + "~");
                    sw.WriteLine(ed.Description);
                    sw.WriteLine("~");
                }
            }

            if (op.Effects != null)
            {
                for (int i = 0; i < op.Effects.Count; i++)
                {
                    EffectData paf = (EffectData)op.Effects[i];
                    sw.WriteLine("Affect " + ((int)paf.ApplyEffectLocation).ToString() + " " +
                      paf.ApplyEffectModifierValue.ToString() + " " + paf.GeneralTypeOfEffect.ToString() + " " +
                      paf.EffectLastsUntil.ToString() + " " + ((int)paf.EffectTypeFlags).ToString());

                }
            }

            if (op.Action_descr != null)
            {
                if (op.Action_descr[0] != null) sw.WriteLine("DescA1 " + op.Action_descr[0] + "~");
                if (op.Action_descr[1] != null) sw.WriteLine("DescA2 " + op.Action_descr[1] + "~");
                if (op.Action_descr[2] != null) sw.WriteLine("DescA3 " + op.Action_descr[2] + "~");
            }

            if (op.How_many != 0) sw.WriteLine("Limtd " + op.How_many.ToString());
            if (!string.IsNullOrEmpty(op.Made_of)) sw.WriteLine("DMat " + op.Made_of);
            sw.WriteLine("Level " + op.Level.ToString());
            sw.WriteLine("LEffect " + ((int)op.Effectbits).ToString());
            if (op.Timer > 0) sw.WriteLine("Timer " + op.Timer.ToString());
            sw.WriteLine("End");
            sw.WriteLine("");
        }

        public void save_objects_old_format(System.IO.StreamWriter sw, AreaData area)
        {
            // Do nothing.. this is done in the area-related file
        }

        public void load_objects_old_format(StreamReader sr, AreaData area)
        {
            area.TotalObjectPrototypes = 0;
            while (sr.EndOfStream == false)
            {
                int vnum;
                char letter = ' ';
                while (letter != '#' && sr.EndOfStream == false) letter = (char)sr.Read();
                if (letter != '#')
                {
                    return;
                }

                vnum = Convert.ToInt32(sr.ReadLine().Trim());
                if (vnum == 0)
                    break;

                if (EmlenMud.WorldTemplates.AllObjPrototypes.ContainsKey(vnum) == true)
                {
                    EmlenMud.LogError("Load_objects: vnum " + vnum.ToString() + " duplicated.", null, LogType.WorldError);
                    continue;
                }

                fread_object_old_format(sr, vnum, area);
                area.TotalObjectPrototypes++;
                if (EmlenMud.WorldTemplates.top_vnum_obj_prototype < vnum) EmlenMud.WorldTemplates.top_vnum_obj_prototype = vnum;
            }
        }
    }
}
